<?php
namespace App\Logic\SalesReport;

use App\Models\SalesReport\PaymentDetail;
use Illuminate\Support\Facades\DB;
class PaymentDetailLing
{
    public static function exePayment($date){
        try{
            $start = time();
            $start_memory = memory_get_usage(); 
            $n = 0;
            PaymentDetail::query()
                ->where('report_date' ,$date)
                ->orderBy('id')
                ->chunk(8000, function ($ss) use(&$n){
                    DB::beginTransaction();
                    foreach ($ss as $_ss){
                        $n++;
                        if($_ss->sales_volume == 0 && 
                            $_ss->sales_amount == 0 && 
                            $_ss->selling_fees == 0 && 
                            $_ss->fba_fees == 0 && 
                            $_ss->refund_num == 0 && 
                            $_ss->refund_amount == 0 && 
                            $_ss->p_thfba == 0 && 
                            (!$_ss->t_purchase_price_rmb || $_ss->t_purchase_price_rmb == 0) && 
                            (!$_ss->t_shipment_price_rmb || $_ss->t_shipment_price_rmb == 0) && 
                            $_ss->ms_fee == 0 && 
                            $_ss->lts_fee == 0 && 
                            $_ss->p_icfee == 0 && 
                            $_ss->rm_fee == 0 && 
                            $_ss->rebates == 0 &&
                            $_ss->spend == 0 &&
                            (!$_ss->reimburse_rmb || $_ss->reimburse_rmb == 0) &&
                            (!$_ss->reimburse_os_rmb || $_ss->reimburse_os_rmb == 0) &&
                            (!$_ss->reimburse_profit_rmb || $_ss->reimburse_profit_rmb == 0) &&
                            $_ss->ied_rmb == 0 &&
                            $_ss->rod_rmb == 0 &&
                            (!$_ss->vat_fee || $_ss->vat_fee == 0) &&
                            (!$_ss->total || $_ss->total == 0) &&
                            (!$_ss->profit_rmb || $_ss->profit_rmb == 0)){
                            $_ss->ling = 1;
                            $_ss->save();
                            echo '行'.$n.'更新成功'.PHP_EOL;
                        }else{
                            echo '行'.$n.'无需更新'.PHP_EOL;
                        }
                    }
                    DB::commit();
                });
            sleep(10);
            DB::beginTransaction();
            PaymentDetail::query()->where('report_date' ,$date)->where('ling', 1)->delete();
            DB::commit();
            $end = time();
            $end_memory = memory_get_usage();
            print_r(($end - $start).'--------'.(($end_memory - $start_memory)/1024/1024)) ;
        }catch (\Exception $e){
            report($e);
        }
    }
}
